package start.spring.basic.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;
import start.spring.basic.exception.common.ExceptionInterface;
import start.spring.basic.response.ResponseData;

/**
 * ****************************************************************************
 * 全局异常处理类，捕获异常后返回错误代码到页面
 *
 * @author(作者)：xuyongyun	
 * @author(复审人)：	Administrator 2018年7月3日
 * @version(版本)1.0
 * @since 1.0
 ******************************************************************************
 */
@Slf4j
@ControllerAdvice
public class GloabExceptionHandler {
	
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResponseData<String> defaultExceptionHandler(HttpServletRequest req,Exception e){
		String errcode = "";
		String errmsg = "";
		if(e instanceof ExceptionInterface) {
			errcode = ((ExceptionInterface) e).getErrcode();
			boolean isReturnMsg = ((ExceptionInterface) e).isReturnMsg();
			if(isReturnMsg) {
				errmsg = e.getMessage();
			}else {
				errmsg = ExceptionUtil.getExceptionDeteil(e, 0);
			}
		}else {
			errcode = "400001";
			errmsg = ExceptionUtil.getExceptionDeteil(e, 0);
		}
		
		log.error("捕获到全局异常!", e);
		
		return ResponseData.error(errcode,errmsg);
	}
}
